<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Minappslists extends Model
{
    protected $table = 'minapps_lists';
    protected $primaryKey = 'id';

    public static $status = ["","可用","试用预授权","试用","等待认证","认证成功","小程序审核中","小程序未通过","待发布小程序"];
    
    public static function getUsersMiniListsLists($authority,$users_id,$name,$appid)
    {
        $query = self::where("users_id",$users_id)->where("disable",1)
        ->when($name,function($query) use ($name){
            $query->where("name","like","%".$name."%");
        })
        ->when($appid,function($query) use ($appid){
            $query->where("appid",$appid);
        });
        return $query->orderBy("id","DESC")->get();
    }

    public static function getMiniListsListsByAppid($appid)
    {
        return self::where("appid",$appid)->first();
    }

    public static function getMiniListsListsById($id)
    {
        return self::where("id",$id)->first();
    }

    public static function getMiniAppByUniqueId($unique_id)
    {
        return self::where("unique_id",$unique_id)->first();
    }

    public static function userBeianMiniappLists($users_id)
    {
        return self::select("appid","name")->where("users_id",$users_id)->whereIn("beian",[1,2,4])->where("disable",1)->orderBy('id','desc')->get();
    }

    public static function fastRegister($users_id,$name,$unique_id,$status)
    {
        return self::insertGetId(["users_id"=>$users_id,"name"=>$name,"unique_id"=>$unique_id,"status"=>$status,"created_at"=>time()]);
    }

    public static function updateMiniappHeadImg($appid,$head_img)
    {
        return self::where("appid",$appid)->update(["head_img"=>$head_img]);
    }

    public static function updateMiniappInfo($appid,$data)
    {
        return self::where("appid",$appid)->update($data);
    }

    public static function fastRegisterAuth($unique_id,$name,$appid_third,$appid,$head_img,$original_id,$qrcode_url,$principal_type,$status)
    {
        return self::where("unique_id",$unique_id)->update(["name"=>$name,"appid_third"=>$appid_third,"appid"=>$appid,"head_img"=>$head_img,"original_id"=>$original_id,"qrcode_url"=>$qrcode_url,"principal_type"=>$principal_type,"status"=>$status]);
    }

    public static function gzhRegisterAuth($users_id,$name,$appid_third,$appid,$head_img,$original_id,$qrcode_url,$status,$create_type)
    {
        return self::insertGetId(["users_id"=>$users_id,"name"=>$name,"appid_third"=>$appid_third,"appid"=>$appid,"head_img"=>$head_img,"original_id"=>$original_id,"qrcode_url"=>$qrcode_url,"status"=>$status,"create_type"=>$create_type,"bind_type"=>1,"created_at"=>time()]);
    }

    public static function secretCreateMiniapp($users_id,$name,$appid,$secret,$original_id,$create_type)
    {
        return self::insertGetId(["users_id"=>$users_id,"name"=>$name,"appid"=>$appid,"secret"=>$secret,"original_id"=>$original_id,"create_type"=>$create_type,"created_at"=>time()]);
    }

    public static function fastVerify($appid,$status,$status_code)
    {
        return self::where("appid",$appid)->update(["status"=>$status,"status_code"=>$status_code]);
    }

    public static function updateStatusCode($id,$appid_third,$appid,$status_code)
    {
        return self::where("id",$id)->update(["appid_third"=>$appid_third,"appid"=>$appid,"status_code"=>$status_code]);
    }

    public static function updateBeianStatus($appid,$beian)
    {
        return self::where("appid",$appid)->update(["beian"=>$beian]);
    }

    public static function updateMinappsListsName($appid,$name)
    {
        return self::where("appid",$appid)->update(["name"=>$name]);
    }

    public static function updateMinappsListsRemark($users_id,$appid,$remark)
    {
        return self::where("appid",$appid)->where('users_id',$users_id)->update(["remark"=>$remark]);
    }

    public static function minappsListsStatus($id,$status)
    {
        return self::where("id",$id)->update(["status"=>$status]);
    }

    public static function minappsListsStatusByAppid($appid,$status)
    {
        return self::where("appid",$appid)->update(["status"=>$status]);
    }

    public static function minappsListsDelete($id,$disable)
    {
        return self::where("id",$id)->update(["disable"=>$disable]);
    }

    public static function updateMinappsInfo($id,$lists)
    {
        return self::where("id",$id)->update($lists);
    }

    public static function minappsListsFasteRegister($id,$appid_third,$appid,$original_id,$qrcode_url,$status)
    {
        return self::where("id",$id)->update(["appid_third"=>$appid_third,"appid"=>$appid,"original_id"=>$original_id,"qrcode_url"=>$qrcode_url,"status"=>$status,"bind_type"=>2]);
    }

    public static function minappsListsStatusByOriginalId($original_id,$status)
    {
        return self::where("original_id",$original_id)->update(["status"=>$status]);
    }

    public static function fastRegisterWeapp($users_id,$name,$status,$create_type)
    {
        return self::insertGetId(["users_id"=>$users_id,"name"=>$name,"status"=>$status,"create_type"=>$create_type,"created_at"=>time()]);
    }

    public static function updateMiniListsSearchStatus($id,$search_status)
    {
        return self::where("id",$id)->update(["search_status"=>$search_status]);
    }

    public static function updateMinappListPayStatus($id,$pay_status)
    {
        return self::where("id",$id)->update(["pay_status"=>$pay_status]);
    }

    public static function usersLastMiniappInfo($users_id)
    {
        return self::select("minapps_lists_info.enterprise_name","minapps_lists_info.code_type","minapps_lists_info.code","minapps_lists_info.legal_persona_wechat","minapps_lists_info.legal_persona_name")
                    ->leftJoin('minapps_lists_info', 'minapps_lists.id', '=', 'minapps_lists_info.minapps_lists_id')
                    ->where("minapps_lists.users_id",$users_id)
                    ->orderBy("minapps_lists.id","DESC")
                    ->first();
    }

    public static function getLastMiniappTime($users_id)
    {
        return self::select("created_at")
                    ->where("users_id",$users_id)
                    ->orderBy("id","DESC")
                    ->first();
    }

    public static function getUserLastMiniList($users_id)
    {
        return self::where("users_id",$users_id)->where("beian","!=",5)->orderBy("id","DESC")->first();
    }

    public static function updateMiniappPayStatus($id,$order_id)
    {
        return self::where("id",$id)->update(["pay_status"=>2,"order_id"=>$order_id]);
    }
}